package com.ccy.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccy.product.dao.ProductAttrValueDao;
import com.ccy.product.dao.SkuSaleAttrValueDao;
import com.ccy.product.dao.SpuInfoDao;
import com.ccy.product.entity.BrandEntity;
import com.ccy.product.entity.CategoryEntity;
import com.ccy.product.entity.SkuInfoEntity;
import com.ccy.product.entity.SpuInfoEntity;
import com.ccy.product.feign.SearchProductFeign;
import com.ccy.product.service.BrandService;
import com.ccy.product.service.CategoryService;
import com.ccy.product.service.SkuInfoService;
import com.ccy.product.service.SpuInfoService;
import com.ccy.product.to.Attr;
import com.ccy.product.to.SkuEsModel;
import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Service("spuInfoService")
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoDao, SpuInfoEntity> implements SpuInfoService {

    @Autowired
    private SkuInfoService skuInfoService;


    @Autowired
    private BrandService brandService;


    @Autowired
    private SkuSaleAttrValueDao skuSaleAttrValueDao;


    @Autowired
    private ProductAttrValueDao spuAttrValueDao;

    // categoryService -->  spuinfoservice   循环依赖   如何解决   lazy如何起作用

    @Autowired
    @Lazy
    private  CategoryService categoryService;


    @Autowired
    private SearchProductFeign searchProductFeign;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpuInfoEntity> page = this.page(
                new Query<SpuInfoEntity>().getPage(params),
                new QueryWrapper<SpuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void spuStatusUp(long spuId) {

        // spuId   ==>  skus
        List<SkuInfoEntity> skus = skuInfoService.getSkusBySpuId(spuId);

        //公共属性
        //BeanUtils.copyProperties(sku,esModel);


        //  skus --> skuesmodels
        List<SkuEsModel> skuEsModels = skus.stream().map(o -> {

            SkuEsModel skuEsModel = new SkuEsModel();
            // 公共属性处理掉了
            BeanUtils.copyProperties(o, skuEsModel);

            //brand
            BrandEntity brand = brandService.getById(o.getBrandId());
            skuEsModel.setBrandName(brand.getName());

            //category
            CategoryEntity category = categoryService.getById(o.getCatalogId());
            skuEsModel.setCatalogName(category.getName());


            //attr  --   有效的可用于查询的属性
            List<Attr> skuSearchableAttrs = skuSaleAttrValueDao.getSearchAbleAttrsBySkuId(o.getSkuId());
            List<Attr> spuSearchableAttrs = spuAttrValueDao.getSpuSearchableAttrsBySpuId(o.getSpuId());

            Set<Attr> attrs = new HashSet<>();
            attrs.addAll(skuSearchableAttrs);
            attrs.addAll(spuSearchableAttrs);

            skuEsModel.setSkuPrice(o.getPrice());

            // list -- array
            // arry --list
            // list -- json
            // json --list ,map ,obj

            //select brandId, avg(price),sum(price),brandName
            // group by brandId,brandName

            skuEsModel.setAttrs(new ArrayList<>(attrs));

            return skuEsModel;
        }).collect(Collectors.toList());

        //hasstock
        //brandname  brandService
        //

        //attrs



        // List<SkuEsModel>
        // 调用searchservice 完成上架   bulk

        searchProductFeign.up(skuEsModels);
    }

}